
import java.util.Random;


public class AI 
{
	private int ailastmove[] = null;     //0 = row, 1 = column
	private int playerlastmove[] = null; //0 = row, 1 = column
	private boolean needtoblock = false;
	
	/** @return true if computer detects the need of blocking the human player**/
	public boolean getNeedtoblock()
	{
		return needtoblock;
	}

	/**needtoblock setter
	 * @param boolean**/
	public void setNeedtoblock(boolean needtoblock) 
	{
		this.needtoblock = needtoblock;
	}

	/**@return the last move of the ai player**/
	public int[] getAilastmove()
	{
		return ailastmove;
	}
	
	/**ailastmove setter
	 * @param int[2] array**/
	public void setAilastmove(int[] ailastmove) 
	{
		this.ailastmove = ailastmove;
	}
	
	/**@return the last move of the human player**/
	public int[] getPlayerlastmove()
	{
		return playerlastmove;
	}
	
	/**playerlastmove setter
	 * @param int[2] array**/
	public void setPlayerlastmove(int[] playerlastmove) 
	{
		this.playerlastmove = playerlastmove;
	}
	
	/**class constructor**/
	public AI()
	{
		int[] x = new int[2];
		x[0] = 5;
		x[1] = 5;
		ailastmove = new int[2];
	    setAilastmove(x);
		playerlastmove = new int[2];
	    setPlayerlastmove(x);
	}
	
	/**looks for the best move the ai can make
	 * @return the number of the most suitable column
	 * @param "col", "row", "diag1" or "diag2"**/
	public int bestmove(String rcd)
	{
		if (getNeedtoblock() == true)
		{
			if (rcd == "col")
				return playerlastmove[1];
			else 
				if (rcd == "row")
				{
					int oszlop = playerlastmove[1];
					if (oszlop < 7)
						while (GameWindowAWT.getBoard().getHoles()[playerlastmove[0]][oszlop].getIs() == 1)
							oszlop++;
					else
						if (oszlop>3)
							while (GameWindowAWT.getBoard().getHoles()[playerlastmove[0]][oszlop].getIs() == 1)
								oszlop--;
						else
							if (oszlop>= 7)
								oszlop++;
							else
								oszlop--;
					return oszlop;
				}
				else
					if (rcd == "diag1")
					{
						int oszlop = playerlastmove[1];
						int sor = playerlastmove[0];
						while (GameWindowAWT.getBoard().getHoles()[sor][oszlop].getIs() == 1)
						{
							oszlop--;
							sor++;
						}
						return oszlop;
					}
					else
					{
						int oszlop = playerlastmove[1];
						int sor = playerlastmove[0];
						while (GameWindowAWT.getBoard().getHoles()[sor][oszlop].getIs() == 1)
						{
							oszlop++;
							sor--;
						}
						return oszlop;
					}
		}
		else
		{
			if (ailastmove != null)
			{
				Random r2 = new Random();
				int[] posib = new int[3];
				int last = ailastmove[1];
				if ((last != 1)&&(last != 10))
				{
					posib[0] = last; //ugyanarra az oszlopra
					posib[1] = last+1; //ugyanarra a  sorra, jobbra
					posib[2] = last-1; //ugyanarra a sorra, balra
				}
				else 
					if (last == 1)
					{
						posib[0] = last;
						posib[2] = last+1;
						posib[3] = last+1;
					}
					else
					{
						posib[0] = last;
						posib[2] = last-1;
						posib[3] = last-1;
					}
				return posib[r2.nextInt(3)];
			}
		}
		return 0;
	}
	
	/**places the coin so that the human player can't win**/
	public void block()
	{
		GameWindowAWT.getBoard().getGame().placeCoin(bestmove(GameWindowAWT.getBoard().getGame().getRcd()));
//		if (GameWindowAWT.getBoard().getGame().getRcd() == "col")
//			GameWindowAWT.getBoard().getGame().placeCoin(playerlastmove[1]);
//		else
//			if (GameWindowAWT.getBoard().getGame().getRcd() == "row")
//				if (GameWindowAWT.getBoard().getHoles()[playerlastmove[0]][playerlastmove[1]-1].getIs() == 0)
//					GameWindowAWT.getBoard().getGame().placeCoin(playerlastmove[1]-1);
//				else 
//					
//			else
//				if (GameWindowAWT.getBoard().getGame().getRcd() == "diag1")
//					GameWindowAWT.getBoard().getGame().placeCoin(playerlastmove[1]-1);
//				else
//					GameWindowAWT.getBoard().getGame().placeCoin(playerlastmove[1]-1);

	}
	
//	public void place()
//	{		
//		Random rand = new Random();
//		int r = rand.nextInt(10);
//		while (r<1)
//			r = rand.nextInt(10);
//		System.out.println(r);
//		GameWindowAWT.getBoard().getGame().placeCoin(r);
//	}
	
	/**places the coin of the ai**/
	public void place()
	{
		GameWindowAWT.getBoard().setPlayer(2);
		int[] playermove = getPlayerlastmove();
		boolean can = GameWindowAWT.getBoard().getGame().detectWinner(playermove[1], playermove[0], 3, 1);
		if (can)
		{
			setNeedtoblock(true);
			block();
		}
		else 
		{
			setNeedtoblock(false);
			GameWindowAWT.getBoard().getGame().placeCoin(bestmove(GameWindowAWT.getBoard().getGame().getRcd()));
		}
		
		//return false;
	}
	
}
